require(MASS)

ImputeMean <- function(x){
  mu <- mean(na.omit(x))
  x[is.na(x)] <- mu
  x
}

Quantilize <- function(x, num.tiles){
  qntls <- unique(quantile(x, seq(0, 1, 1/num.tiles)))
  if (length(qntls) > 1){
    cut(x, qntls, 1:(length(qntls) - 1), include.lowest=T)
  } else {
    as.factor(rep(1, length(x)))
  }
}


#Read and mean impute the training data
setwd("~/accept_decline")
df.train <- readRDS("train.rds")
df.train <- data.frame(lapply(df.train, ImputeMean))
df.train <- data.frame(df.train, loss.ind=df.train$loss>0)

#Compute the Tantrev variable
m.tantrev <- glm(formula = loss.ind ~ f274 + f528, family = binomial(), data = df.train)
pred.tantrev <- predict(m.tantrev, type="response")
a <- Quantilize(pred.tantrev, 100)
plot(tapply(df.train$loss.ind, a, mean))
sum(df.train$loss.ind[pred.tantrev>.09])/sum(df.train$loss.ind)

#Subset to highest Tantrev values
df.subset <- df.train[pred.tantrev > .09,]
pred.tantrev.subset <- pred.tantrev[pred.tantrev > .09]
a <- Quantilize(pred.tantrev.subset, 20)
df.quantile <- lapply(df.subset[,2:(ncol(df.subset) - 2)], Quantilize, 20)
df.quantile <- data.frame(df.quantile, tantrev = a, loss.ind=df.subset$loss.ind,
                          loss=df.subset$loss)




m.try <- glm(loss.ind ~ tantrev + f2 + f4 + f5 + f10 + f13 + f14 + f25 + f42 + f57 + f64 + f67 + f73 + f152 + f214 + f229 + f254 + f284 + f293 + f300, data=df.quantile)

addterm(m.try, ~ . +f201 + f202 + f203 + f204 + f205 + f206 + f207 + f208 + f209 + f210 + f211 + f212 + f213 + f214 + f215 + f216 + f217 + f218 + f219 + f220 + f221 + f222 + f223 + f224 + f225 + f226 + f227 + f228 + f229 + f230 + f231 + f232 + f233 + f234 + f235 + f236 + f237 + f238 + f239 + f240 + f241 + f242 + f243 + f244 + f245 + f246 + f247 + f248 + f249 + f250 + f251 + f252 + f253 + f254 + f255 + f256 + f257 + f258 + f259 + f260 + f261 + f262 + f263 + f264 + f265 + f266 + f267 + f268 + f269 + f270 + f271 + f272 + f273 + f274 + f275 + f276 + f277 + f278 + f279 + f280 + f281 + f282 + f283 + f284 + f285 + f286 + f287 + f288 + f289 + f290 + f291 + f292 + f293 + f294 + f295 + f296 + f297 + f298 + f299 + f300   , test="Chisq")


